package com.hzqy.service.special;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzqy.commons.utils.FileUtils;
import com.hzqy.commons.utils.PublicUtil;
import com.hzqy.persistence.dao.special.SpecialDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.service.picture.PictureType;
import com.hzqy.web.vo.PmsUserVo;
import com.hzqy.web.vo.SysProductVo;
import com.hzqy.web.vo.special.SpecialInfoVo;
import com.hzqy.web.vo.special.SpecialViewVo;

@Service
public class SpecialServiceBean extends GenericServiceBean<PmsUserVo, SpecialDao, Integer>{
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private SpecialDao specialDao;
	@Override
	public SpecialDao getEntityDao() {
		return this.specialDao;
	}
	public int specialViewDataUpdate(JSONObject jsonObject) {
		int specialid=jsonObject.getIntValue("specialid");
		String specialname=jsonObject.getString("specialname");
		String resolution=jsonObject.getString("resolution");
		LOG.debug("specialid:"+specialid+"--specialname:"+specialname+"--resolution:"+resolution);
		SpecialInfoVo specialInfoVo=new SpecialInfoVo();
		specialInfoVo.setF_si_name(specialname);
		specialInfoVo.setF_si_resolution(resolution);
		//是想把所有的都删除了重新添加吗？这样有什么不妥的吗？这样对应的专辑ID就会变了，如果已经把专辑id给推荐出去了，那这样就不好了。
		//根据id修改也不行，
		String viewstr=jsonObject.getString("view");
		LOG.debug("specialViewDataUpdate-viewstr:"+viewstr);
		int deleteFlag=deleteSpecialViewById(specialid);
		if(deleteFlag>=0){
			JSONArray jsonArray = (JSONArray) jsonObject.get("view");
			String jsonArrayStr=JSON.toJSONString(jsonArray);
			List<Object> list =JSON.parseArray(jsonArrayStr);
			if(list != null && list.size() > 0){
				SpecialViewVo vo=new SpecialViewVo();
				String fsvinfo="";//视频按钮对应的类型
				for(int i=0;i<list.size();i++){
					Map<String,Object> map = (Map<String, Object>) list.get(i);
					JSONObject objdata =(JSONObject) JSON.toJSON(map);
					vo.setF_sv_siid(specialid);
					vo.setF_sv_x(objdata.getInteger("x"));
					vo.setF_sv_y(objdata.getInteger("y"));
					vo.setF_sv_width(objdata.getInteger("w"));
					vo.setF_sv_height(objdata.getInteger("h"));
					vo.setF_sv_pic(objdata.getString("picpath"));
					vo.setF_sv_type(objdata.getString("type"));
					fsvinfo= objdata.getString("videoType");
					LOG.debug("fsvinfo:"+fsvinfo);
					vo.setF_sv_info(fsvinfo);//当视频类型来用，1：其他，  2：音乐 ，3：教育
					vo.setF_sv_zindex(objdata.getIntValue("zindex"));
					String fdxs=objdata.getString("fdxs");
					Pattern pattern = Pattern.compile("[0-9]*");
					Matcher isNum = pattern.matcher(fdxs);
					if( !isNum.matches() ){
						fdxs="0";
					}
					vo.setF_sv_fdxs(objdata.getString("fdxs"));
					vo.setF_sv_urls(objdata.getString("url"));
					specialDao.insertSpecialView(vo);//Vo赋值
				}
			}else{//没有上传数据
				LOG.debug("没有上传数据！");
			}
		}
		return 0;
	}

	private int deleteSpecialViewById(int specialid){
		int counts=-1;
		counts=specialDao.deleteSpecialViewById(specialid);
		return counts;
	}
	public int insertSpecial(SpecialInfoVo specialInfoVo) {
		return specialDao.insertSpecial(specialInfoVo);
	}
	public SpecialInfoVo selectSpecial(String f_si_id) {
		SpecialInfoVo specialInfoVo=specialDao.selectSpecial(f_si_id);
		return specialInfoVo;
	}

	public Map<String,Object> specialList(SpecialInfoVo specialInfoVo,int pageNo,int pageSize) {
		int rowCount=specialDao.selectSpecialCount(specialInfoVo);
		Map<String, Object>map=new HashMap<String, Object>();
		map.put("total", rowCount);
		if(rowCount>0 && pageNo>0 && pageSize>0){
			map.put("firstResult", (pageNo-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_si_name", specialInfoVo.getF_si_name());
			map.put("f_si_status", specialInfoVo.getF_si_status());
			map.put("f_si_startdate", specialInfoVo.getF_si_startdate());
			map.put("f_si_enddate", specialInfoVo.getF_si_enddate());
			List<SpecialInfoVo> list=specialDao.selectSpecialList(map);
			map.put("rows", list);
		}else{
			map.put("rows", new ArrayList<SpecialInfoVo>());
		}
		return map;
	}
	public int updateSpecial(SpecialInfoVo specialInfoVo) {
		return specialDao.updateSpecial(specialInfoVo);
	}
	public int deleteSpecial(int f_si_id) {
		int falg= specialDao.deleteSpecial(f_si_id);
		return falg;
	}
	public List<SpecialViewVo> selctSpecialViewList(String specialid) {
		return specialDao.selctSpecialViewList(specialid);
	}

	public String getFilePath(String picType) {
		String imgFile = "PIC/imgs/upload/"+PictureType.getPicType(picType)+"/"+PublicUtil.getCurDate()+"/";
		return imgFile;
	}

	public String getFileName(String uploadifyFileName) {
		//Random random = new Random();
		SecureRandom random = new SecureRandom();
		int ranValue = random.nextInt(10000);
		String fileName = PublicUtil.getGenFileName()+ranValue+"."+FileUtils.getExtendName(uploadifyFileName);
		return fileName;
	}

	public List<SpecialInfoVo> selectSpecialListExcel(SpecialInfoVo specialInfoVo) {
		Map<String, Object>map=new HashMap<String, Object>();
		map.put("f_si_name", specialInfoVo.getF_si_name());
		map.put("f_si_status", specialInfoVo.getF_si_status());
		map.put("f_si_startdate", specialInfoVo.getF_si_startdate());
		map.put("f_si_enddate", specialInfoVo.getF_si_enddate());
		return specialDao.selectSpecialListExcel(map);
	}
	public List<SysProductVo> selectSysProduct() {
		return specialDao.selectSysProduct();
	}
}
